package com.hehe.boot.controller;

import com.hehe.boot.pojo.Reservation;
import com.hehe.boot.pojo.ReservationHttp.*;
import com.hehe.boot.pojo.Result;
import com.hehe.boot.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/reservation")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    @PostMapping("/add")
    public Result<ReservationAddResponse> Add(@RequestBody ReservationAddRequest request){
        Long reservationId = reservationService.addReservation(request);
        if (reservationId == -1){
            return Result.error("AddReservation error: doctor did`t exist");
        }
        if (reservationId == -2){
            return Result.error("AddReservation error: fail to save reservation");
        }
        ReservationAddResponse response = new ReservationAddResponse();
        response.setReservationId(reservationId);
        return Result.success(response);
    }

    @PostMapping("/userListReservation")
    public Result<List<ReservationUserListResponse>> UserList(@RequestBody ReservationUserListRequest request){
        List<ReservationUserListResponse> responses =  reservationService.userList(request.getUserId());
        return Result.success(responses);
    }

    @PostMapping("/doctorListReservation")
    public Result<List<ReservationDoctorListResponse>> DoctorList(@RequestBody ReservationDoctorListRequest request){
        List<ReservationDoctorListResponse> responses =  reservationService.doctorList(request.getDoctorId());
        return Result.success(responses);
    }

    @GetMapping("/getById")
    public Result<Reservation> GetById (@RequestParam String id){
        return Result.success(reservationService.getById(id));
    }

    @DeleteMapping("/{id}")
    public Result DeleteReservation(@PathVariable Long id){
        Boolean flag = reservationService.removeById(id);
        if (flag == false){
            return Result.error("DeleteReservation error: fail to delete reservation");
        }
        return Result.success();
    }
}
